using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI;
using ICSharpCode.TextEditor;
using ICSharpCode.TextEditor.Gui.CompletionWindow;
using ICSharpCode.TextEditor.Document;
using ICSharpCode.TextEditor.Actions;
using Pub;
using WeifenLuo.WinFormsUI.Docking;

namespace Easily
{
    //图形化代码窗体包括main.c、include.c、isr.c
    public partial class frmGraphicCode : DockContent
    {
        //frmGraphicCode类的字段
        //创建存放三个文件代码的文本框
        public ICSharpCode.TextEditor.TextEditorControl text_main = new ICSharpCode.TextEditor.TextEditorControl();//存放main.c代码的文本框
        public ICSharpCode.TextEditor.TextEditorControl text_isr = new ICSharpCode.TextEditor.TextEditorControl();//存放isr.c代码的文本框
        public ICSharpCode.TextEditor.TextEditorControl text_includes = new ICSharpCode.TextEditor.TextEditorControl();//存放includes.h代码的文本框
        public CTextEditMenu tboxMenu_main = null;
        public CTextEditMenu tboxMenu_isr = null;
        public CTextEditMenu tboxMenu_includes = null;

        //FormCode类的构造函数
        public frmGraphicCode()
        {
            InitializeComponent();

            this.CloseButton = false;//隐藏窗体的关闭按钮
            this.AutoHidePortion = 0.22;//窗体停靠时的宽度是屏幕宽度的0.2倍
            //设置存放C代码的文本框的属性,并将该文本框添加到窗体中
            setText(text_main);
            setText(text_isr);
            setText(text_includes);
            addRightClick();
            this.tpg_main.Controls.Add(text_main);
            this.tpg_isr.Controls.Add(text_isr);
            this.tpg_includes.Controls.Add(text_includes);
            text_main.Refresh();
            text_isr.Refresh();
            text_includes.Refresh();
        }

        private void setText(ICSharpCode.TextEditor.TextEditorControl text)
        {
            text.Language = TextEditorControl.Languages.C;
            //text.Document.HighlightingStrategy = HighlightingStrategyFactory.CreateHighlightingStrategy("C++.NET");
            text.Encoding = System.Text.Encoding.Default;
            text.EnableFolding = false;
            text.TextEditorProperties.IsIconBarVisible = false;
            text.Font = new System.Drawing.Font("宋体", 12,
                System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            text.ForeColor = Color.Black;
            text.Dock = System.Windows.Forms.DockStyle.Fill;
            text.Location = new System.Drawing.Point(0, 0);
            text.Name = "richTextBoxCode";
            text.Size = new System.Drawing.Size(800, 480);
            text.TabIndex = 0;
            text.Document.ReadOnly = true;
            text_main.TextEditorProperties.ShowSpaces = false;
            text.EnableFolding = true;
            text.IsReadOnly = true;
            //绑定事件
            //text.ActiveTextAreaControl.TextArea.KeyDown += new System.Windows.Forms.KeyEventHandler(keyDown_event);
        }
        //设置窗体代码是否为只读
        public void setReadOnly(bool value)
        {
            text_main.IsReadOnly = value;
            text_isr.IsReadOnly = value;
            text_includes.IsReadOnly = value;
        }
        //添加右击菜单
        public void addRightClick()
        {
            tboxMenu_main = new CTextEditMenu(text_main);
            tboxMenu_isr = new CTextEditMenu(text_isr);
            tboxMenu_includes = new CTextEditMenu(text_includes); 
        }

        public void removeRightClick()
        {
            text_main.ContextMenuStrip = null;
            text_isr.ContextMenuStrip = null;
            text_includes.ContextMenuStrip = null;
        }
        //void keyDown_event(object sender, KeyEventArgs e)
        //{
        //    ICSharpCode.TextEditor.TextEditorControl m = sender as ICSharpCode.TextEditor.TextEditorControl;
        //    //按下ctrl+F
        //    if (e.Modifiers.CompareTo(Keys.Control) == 0 && e.KeyCode == Keys.F)
        //    {
        //        m.CM_find.PerformClick();
        //        //处理逻辑
        //        //MessageBox.Show("OK");
        //    }
        //}


    }
}